#include <assert.h>
#include "ai.h"
#include "character.h"
#include "point2.h"
#include "globals.h"

// move from xLo to xHi and back
bool aiLine(Character* character){//, float xLo, float xHi){

	assert(character);

	character->moveXminus =false;
	character->moveXplus =false;

	character->getCenter(&V_TMP);
	if(character->orientation->x <0){

		if(V_TMP.x > character->ai->xLo)
			character->moveXminus =true;
		else
			character->moveXplus =true;
	}
	else{
		if(V_TMP.x < character->ai->xHi)
			character->moveXplus =true;
		else
			character->moveXminus =true;
	}

	return true;
}

// chaser follows the runner holding the distance dx
bool aiFollowMe(Character* runner, Character* chaser){//, float dx){

	assert(runner);
	assert(chaser);

	float alt, altp;
	Point2 runner_pos, chaser_pos;	//?O globals

	chaser->moveXminus =false;
	chaser->moveXplus =false;

	altp =(chaser->get_yHi()-chaser->get_yLo())*0.33f;
	alt =runner->get_yLo() -chaser->get_yLo();
	if( !(alt<altp && alt>-altp) ) //? hardcoded
		return false;

	runner->getCenter(&V_TMP);
	if(V_TMP.x <=chaser->ai->xLo)
		return false;
	if(V_TMP.x >=chaser->ai->xHi)
		return false;

	runner->getCenter(&runner_pos);
	chaser->getCenter(&chaser_pos);
	if(chaser_pos.x < runner_pos.x){

			chaser->moveXplus =true;
			chaser->orientation->x =1;
			return true;
	}
	else if(chaser_pos.x > runner_pos.x){

			chaser->moveXminus =true;
			chaser->orientation->x =-1;
			return true;
	}

	return false;
}

// attacker attacks targed if it is closer than range
bool aiAttack(Character* target, Character* attacker){//, float range){

	float alt, altp;

	attacker->attack =false;

	altp =(attacker->get_yHi()-attacker->get_yLo())*0.33f;
	alt =target->get_yLo() -attacker->get_yLo();
	if(alt<altp && alt>-altp){ //? hardcoded

		if(target->get_xLo()>attacker->get_pic_xLo() && target->get_xLo()<attacker->get_pic_xHi()){ //?
			attacker->attack =true;	
			return true;
		}
		else if(target->get_xHi()<attacker->get_pic_xHi() && target->get_xHi()>attacker->get_pic_xLo()){ //?O 
			attacker->attack =true;
			return true;
		}
	}

	return false;
}
